<?php

function reequipment_adminapi_createmaintenance($args)
{
    extract($args);

    // Argument check - make sure that all required arguments are present,
    // if not then set an appropriate error message and return
    $invalid = array();
    if (!isset($equipment_id) || !is_int($equipment_id)) {
        $invalid[] = 'equipment_id';
    } 
    if (!isset($company_id) || !is_int($company_id)) {
        $invalid[] = 'company_id';
    } 
    if (!isset($issue) || !is_string($issue)) {
        $invalid[] = 'issue';
    } 
    if (count($invalid) > 0) {
        $msg = xarML('Invalid #(1)', join(', ', $invalid));
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    }
    // Assume that when this is entered is when it was started.
    $date_scheduled = time();
    // Set the vars that probably will not be here.
    if (!isset($schedule_id)){
    	$schedule_id = '';
    }
    if (!isset($resolution)){
    	$resolution = '';
    }    
    if (!isset($item_ids)){
    	$item_ids = '';
    }  
    if (!isset($manhours)){
    	$manhours = '';
    }  
    if (!isset($downtime)){
    	$downtime = '';
    }  
    if (!isset($date_complete)){
    	$date_complete = '';
    }  
    if (!isset($date_due)){
    	$date_due = '';
    }  
    if (!isset($cycles_due)){
    	$cycles_due = '';
    }  
    
    /* Fields in table.
		maintenance_id
		equipment_id
		company_id
		schedule_id
		frequency_id
		date_scheduled
		date_complete
		issue
		resolution
		item_ids
		manhours
		downtime
   */
    
    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['remaintenance'];
    // Get next ID in table
    $nextId = $dbconn->GenId($table);

    // Add item
    $query = "INSERT INTO $table ("
        . " maintenance_id,
			equipment_id,
			company_id,
			schedule_id,
			frequency_id,
			date_scheduled,
			date_due,
			cycles_due,
			date_complete,
			issue,
			resolution,
			item_ids,
			manhours,
			downtime)"
        . " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $result =& $dbconn->Execute($query, array($nextId, 
											$equipment_id,
											$company_id,
											$schedule_id,
											$frequency_id,
											$date_scheduled,
											$date_due,
											$cycles_due,
											$date_complete,
											$issue,
											$resolution,
											$item_ids,
											$manhours,
											$downtime));
    if (!$result) return;
    // Get the ID of the item that we inserted
    $id = $dbconn->PO_Insert_ID($table, 'maintenance_id');
    
    // Need to record the cycles from this piece of equipment for later reporting and analysis
    $cycles = xarModAPIFunc('reequipment', 'admin', 'createrelcycles', array('equipment_id' => $equipment_id, 'maintenance_id' => $id));

    // Let any hooks know that we have created a new forum
    $args['module'] = 'reequipment';
    $args['itemtype'] = 6; // post
    $args['itemid'] = $id; // post
    xarModCallHooks('item', 'create', $id, $args);// post
    // Return the id of the newly created link to the calling process
    return $id;
}
?>